package com.jltx.zeus.viewmodel.livedata

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer

/**
 * @author jltxseo
 *         Created by junlintianxia on 2020/05/12.
 */
/**
 * A SingleLiveEvent used for Toast messages. Like a [SingleLiveEvent] but also prevents
 * null messages and uses a custom observer.
 *
 *
 * Note that only one observer is going to be notified of changes.
 */
class ToastMessage : SingleLiveEvent<String?>() {
    fun observe(
        owner: LifecycleOwner, observer: ToastObserver
    ) {
        super.observe(owner, Observer { t ->
            if (t == null) {
                return@Observer
            }
            observer.onNewMessage(t)
        })
    }

    interface ToastObserver {
        /**
         * Called when there is a new message to be shown.
         *
         * @param msg The new message
         */
        fun onNewMessage(msg: String?)
    }
}
